<?php

namespace app\proxy\model\page;

use app\common\model\page\Page as PageModel;
use app\proxy\model\proxy\Region;
use app\shop\model\app\App;
use app\proxy\model\auth\User as ProxyUser;

class Page extends PageModel
{
    public static function getHomeDefault($region_id, $proxy_id)
    {
        $region = (new Region())->where("is_delete", '=', 0)
            ->where("proxy_id", '=', $proxy_id)
            ->where("level", '<', 3)
            ->where("region_id", '=', $region_id)
            ->field(['province_id', 'city_id', 'region_id', 'level'])
            ->find();
        if (empty($region)) {
            return self::getDefault();
        }
        $page = (new static())->where('page_type', '10')
            ->where([
                "province_id" => $region["province_id"],
                "city_id" => $region["city_id"],
                "is_audit" => 1
            ])->order('is_default desc,page_id desc')->find();
        if (empty($page) && $region["city_id"] > 0) {
            $page = (new static())->where('page_type', '10')
                ->where([
                    "province_id" => $region["province_id"],
                    "city_id" => 0,
                    "is_audit" => 1
                ])->order('is_default desc,page_id desc')->find();
        }
        return $page ?: self::getDefault();
    }

    /**
     * 获取列表
     */
    public function getList($params, $page_type = 20, $regionIds = [], $proxyId = 0)
    {
        return $this->where(['is_delete' => 0])
            ->where(['page_type' => $page_type])
            ->where(function ($query) use ($params) {
                if (!empty($params["region_id"])) {
                    $query->whereOr("province_id", $params["region_id"])->whereOr("city_id", $params["region_id"]);
                }
            })->where(function ($query) use ($params, $regionIds, $proxyId) {
                if (empty($params["proxy_id"])) {
                    $query->whereOr("province_id", "in", $regionIds)->whereOr("city_id", "in", $regionIds)->whereOr("proxy_id", $proxyId);
                } else {
                    $query->where("proxy_id", $params["proxy_id"]);
                }
            })
            ->withAttr('proxy_name', function ($value, $data) use ($proxyId) {
                return Region::getProxyName($data, $proxyId);
            })
            ->order(['create_time' => 'desc'])
            ->hidden(['page_data'])
            ->paginate($params);
    }

    /**
     * 获取所有自定义页面
     */
    public function getLists($params=[])
    {
        if($params){
            return $this->where(['is_delete' => 0])
                ->where($params)
                ->where(['page_type' => 20])
                ->hidden(['page_data'])
                ->order(['create_time' => 'desc'])
                ->select();
        }
        return $this->where(['is_delete' => 0])
            ->where(['page_type' => 20])
            ->hidden(['page_data'])
            ->order(['create_time' => 'desc'])
            ->select();
    }

    /**
     * 新增页面
     */
    public function add($data, $page_type = 20, $proxy_id = 0, $region_id = 0)
    {
        $region = (new Region())->where("is_delete", '=', 0)
            ->where("proxy_id", '=', $proxy_id)
            ->where("level", '<', 3)
            ->where("region_id", '=', $region_id)
            ->field(['province_id', 'city_id', 'region_id', 'level'])
            ->find();
        if (empty($region)) {
            $this->error = "你无权操作此地区";
            return false;
        }

        $nosh = ProxyUser::checkauth($proxy_id,'page');
        $savedata = [
            'page_type' => $page_type,
            'page_name' => $data['page']['params']['name'],
            'page_data' => $data,
            'app_id' => self::$app_id,
            'proxy_id' => $proxy_id,
            'province_id' => $region["province_id"],
            'city_id' => $region["city_id"]
        ];
        if($nosh){
            $savedata['is_audit'] = 1;
        }else{
            $savedata['is_audit'] = 0;
        }
        // 删除app缓存
        App::deleteCache();
        return $this->save($savedata);
    }

    /**
     * 更新页面
     */
    public function edit($data,$proxy_id=0)
    {
        $nosh = ProxyUser::checkauth($proxy_id,'page');
        if($nosh){
            $savedata = [
                'page_name' => $data['page']['params']['name'],
                'page_data' => $data,
                'is_audit' => 1,
            ];
        }else{
            $savedata = [
                'page_name' => $data['page']['params']['name'],
                'page_data' => $data,
                'is_audit' => 0,
                'is_default' => 0,
            ];
        }
        // 删除app缓存
        App::deleteCache();
        // 保存数据
        return $this->save($savedata) !== false;
    }

    /**
     * 删除
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }

    /**
     * 设置默认页
     */
    public function setDefault()
    {
        if ($this["is_audit"] != 1) {
            $this->error = "未审核或审核不通过不允许设置为默认";
            return false;
        }
        $this->save(['is_default' => 1]);
        $this->where('page_id', '<>', $this['page_id'])->where(["province_id" => $this["province_id"], "city_id" => $this["city_id"]])->update(['is_default' => 0]);
        return true;
    }
}